package com.huawei.hianalytics.f.b;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import com.huawei.hianalytics.f.g.d;
import com.huawei.hianalytics.f.g.h;
import com.huawei.hianalytics.g.b;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class a extends c {
    private Context j;

    public a(Context context) {
        this.j = context;
    }

    public static Map<String, Map<String, List<c>>> a(SharedPreferences sharedPreferences, Context context, String str, boolean z) {
        if (sharedPreferences == null || context == null) {
            return null;
        }
        Map<String, Map<String, List<c>>> hashMap = new HashMap();
        if (z) {
            Map b = h.b(sharedPreferences);
            if (b.size() > 200 || b.size() == 0) {
                b.c("ActionData", "get state data ：The number of data obtained is too much! or No data");
                return hashMap;
            }
            for (Entry a : b.entrySet()) {
                a(a, context, hashMap);
            }
        } else {
            a(str, h.a(sharedPreferences, str), context, (Map) hashMap);
        }
        return hashMap;
    }

    private static void a(String str, String str2, Context context, Map<String, Map<String, List<c>>> map) {
        try {
            if (TextUtils.isEmpty(str2)) {
                b.c("ActionData", "No data from cache sp!");
                return;
            }
            Map hashMap = new HashMap();
            JSONArray jSONArray = new JSONArray(str2);
            int length = jSONArray.length();
            for (int i = 0; i < length; i++) {
                JSONObject optJSONObject = jSONArray.optJSONObject(i);
                c aVar = new a(context);
                aVar.a(optJSONObject);
                if (TextUtils.isEmpty(aVar.i)) {
                    c.a(hashMap, aVar, "noExHashFlag");
                } else {
                    c.a(hashMap, aVar, aVar.i);
                }
            }
            map.put(str, hashMap);
        } catch (JSONException e) {
            b.c("ActionData", "readDataToAppAction() events is not json format");
        }
    }

    private static void a(Entry<String, String> entry, Context context, Map<String, Map<String, List<c>>> map) {
        a((String) entry.getKey(), (String) entry.getValue(), context, (Map) map);
    }

    public static c[] a(Context context, String str) {
        SharedPreferences d = h.d(context, "cached_v2_1");
        return (d == null || TextUtils.isEmpty(str)) ? new c[0] : b(context, h.a(d, str));
    }

    public static c[] b(Context context, String str) {
        if (TextUtils.isEmpty(str)) {
            return new c[0];
        }
        try {
            JSONArray jSONArray = new JSONArray(str);
            List arrayList = new ArrayList();
            for (int i = 0; i < jSONArray.length(); i++) {
                JSONObject jSONObject = jSONArray.getJSONObject(i);
                a aVar = new a(context);
                aVar.a(jSONObject);
                arrayList.add(aVar);
            }
            return (c[]) arrayList.toArray(new c[arrayList.size()]);
        } catch (JSONException e) {
            b.b("ActionData", "cache data is not jsonArray");
            return new c[0];
        }
    }

    public JSONObject a() {
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("type", g());
            jSONObject.put("eventtime", this.a);
            jSONObject.put("event", this.b);
            jSONObject.put("event_session_name", this.g);
            jSONObject.put("first_session_event", this.h);
            jSONObject.put("nc_common_flag", this.i);
            String b = d.b(h(), this.j);
            this.d = (long) b.length();
            jSONObject.put("content", b);
        } catch (JSONException e) {
            b.c("ActionData", "When toJsonStr() executed,properties parameter anomaly.JSON Exception has happen!");
        }
        return jSONObject;
    }

    public JSONObject a(boolean z, boolean z2) {
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("type", g());
            jSONObject.put("eventtime", this.a);
            jSONObject.put("event", this.b);
            jSONObject.put("event_session_name", this.g);
            jSONObject.put("first_session_event", this.h);
            if (z2) {
                jSONObject.put("nc_common_flag", this.i);
            }
            if (z) {
                String b = d.b(h(), this.j);
                this.d = (long) b.length();
                jSONObject.put("properties", b);
                return jSONObject;
            }
            jSONObject.put("properties", new JSONObject(this.c));
            return jSONObject;
        } catch (JSONException e) {
            b.c("ActionData", "When toJsonObj() executed,properties parameter anomaly.JSON Exception has happen!");
            return null;
        }
    }

    public void a(JSONObject jSONObject) {
        if (jSONObject != null) {
            this.a = jSONObject.optString("eventtime", "");
            this.b = jSONObject.optString("event", "");
            f(d.a(jSONObject.optString("properties"), this.j));
            this.e = jSONObject.optString("type", "");
            if (jSONObject.has("event_session_name")) {
                this.g = jSONObject.optString("event_session_name");
                this.h = jSONObject.optString("first_session_event");
            }
            this.i = jSONObject.optString("nc_common_flag");
        }
    }

    public JSONObject b() {
        return a(false, false);
    }
}
